package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;

import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRulesBusiness;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.salesmanage.util.DepositOrderDetailDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 同一实名制个人客户，N天内卡充值超过N元
 * @author ZXL
 *
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmRule4")
public class AlarmRule4 implements AlarmRulesBusiness {
	
	@EJB(mappedName = "salesmanage.impl.DepositOrderDetailDaoImpl")
	private DepositOrderDetailDao depositOrderDetailDao;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
	private AlarmInfoDetailDao alarmInfoDetailDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;
	
	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void runAlarmRules(String issueCompanyNo, Date tranDate,
			AlarmRule alarmRule) throws PCPSApplicationException {
		PCPSUtil.Log("执行预警规则:同一实名制个人客户，N天内卡充值超过N元");
		// 取预警参数
		if (tranDate == null || alarmRule == null
				|| PCPSUtil.isEmpty(alarmRule.getRuleParameter())) {
			PCPSUtil.Log("预警规则为空或交易日期为空!");
			return;
		}
		String[] parameters = alarmRule.getRuleParameter().split(":");
		if (parameters.length < 2 || PCPSUtil.isEmpty(parameters[0])
				|| PCPSUtil.isEmpty(parameters[1])) {
			PCPSUtil.Log("预警规则参数为空!");
			return;
		}
		if (!PCPSUtil.isNumeric(parameters[0]) || !PCPSUtil.isNumeric(parameters[1])) {
			PCPSUtil.Log("预警规则参数错误!");
			return;
		}
		String startDate = PCPSUtil.getSubductionDays(
				PCPSUtil.getStringYMDByDate(tranDate), -Integer.parseInt(parameters[0]));
		String endDate =  PCPSUtil.getStringYMDByDate(tranDate);
		
		releaseAlarm(issueCompanyNo,alarmRule,startDate,endDate,new BigInteger(parameters[1]));

	}
	
	/**
	 * 实名人充值信息预处理
	 * @param issueCompanyNo
	 * @param alarmRule
	 * @param startDate
	 * @param endDate
	 * @param amount
	 * @throws PCPSApplicationException
	 */
	private void releaseAlarm(String issueCompanyNo, AlarmRule alarmRule,
			String startDate, String endDate, BigInteger amount)
					 throws PCPSApplicationException{
		//根据预警条件获取信息
		List<Object[]> doList = depositOrderDetailDao.
				getDepositOrderCardByAlarmRule4(issueCompanyNo,startDate,
						endDate);
		//实名人充值总金额
		Map<String,BigInteger> mapCustomId = new HashMap<String,BigInteger>();
		//实名人充值信息
		Map<String,List<Object[]>> mapList = new HashMap<String,List<Object[]>>();
		//处理实名人信息
		for (Object[] obj : doList){
			String customId = obj[1].toString();
			BigInteger depositAmount = new BigInteger(obj[2].toString());
			String customName = obj[3].toString();
			String saleBranchNo = obj[4].toString();
			if (mapCustomId.get(customId) == null){
				mapCustomId.put(customId,depositAmount);
				List<Object[]>  objList = new ArrayList<Object[]>();
				objList.add(obj);
				mapList.put(customId, objList);
				if (depositAmount.compareTo(amount) > 0 ){
					insertAlarmInfoAndDetail(issueCompanyNo,alarmRule,saleBranchNo,
							customName,objList);
				}
			}else{
				BigInteger deposit = mapCustomId.get(customId);
				deposit = deposit.add(depositAmount);
				mapCustomId.put(customId, deposit);
				List<Object[]>  objList = mapList.get(customId);
				objList.add(obj);
				mapList.put(customId, objList);
				if (deposit.compareTo(amount) > 0 ){
					insertAlarmInfoAndDetail(issueCompanyNo,alarmRule,saleBranchNo,
							customName,objList);
				}
			}
		}
	}
	
	/**
	 * 预警信息插入
	 * @param issueCompanyNo
	 * @param alarmRule
	 * @param saleBranchNo
	 * @param customName
	 * @param objList
	 * @throws PCPSApplicationException
	 */
	public void insertAlarmInfoAndDetail(String issueCompanyNo, AlarmRule alarmRule,
			String saleBranchNo,String customName,List<Object[]>  objList)
					throws PCPSApplicationException{
		//获取销售机构
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(saleBranchNo);
		//获取主键
		BigInteger alarmInfoId =  pcpsCommonDaoImpl.getUniqueIntegerKey();
		//插入预警信息
		AlarmInfo alarmInfo = new AlarmInfo();
		alarmInfo.setAlarmInfoId(alarmInfoId);
		alarmInfo.setAlarmRuleId(alarmRule.getAlarmRuleId());
		alarmInfo.setIssueCompanyNo(issueCompanyNo);
		alarmInfo.setAlarmTime(commonDaoImpl.getDatabaseSysTime());
		alarmInfo.setBranchNo(saleBranchNo);
		alarmInfo.setBranchName(saleBranch.getSaleBranchName());
		alarmInfo.setRiskCustomName(customName);
		alarmInfo.setAlarmReason(alarmRule.getAlarmRuleDesc());
		alarmInfo.setDealStatus(AlarmInfo.ALARMINFO_DEALSTATUS_UNTREATED);
		alarmInfoDaoImpl.insertAlarmInfo(alarmInfo);
		//插入预警明细信息
		AlarmInfoDetail alarmInfoDetail = null;
		int seqno = 1;
		for (Object[] obj : objList){
			BigInteger depositOrderId = new BigInteger(obj[0].toString());
			String trandate = obj[5].toString();
			alarmInfoDetail = new AlarmInfoDetail();
			alarmInfoDetail.setAlarmInfoId(alarmInfoId);
			alarmInfoDetail.setSeqNo(seqno);
			alarmInfoDetail.setBusinessType(AlarmInfoDetail.ALARMINFODETAIL_BUSINESSTYPE_SALEORDER);
			alarmInfoDetail.setBusinessOrderId(depositOrderId);
			alarmInfoDetail.setTranDate(PCPSUtil.getDateYMDByString(trandate));
			alarmInfoDetailDaoImpl.insertAlarmInfoDetail(alarmInfoDetail);
			seqno++;
		}
	}
}
